package com.mimosa.common.system.user;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.mimosa.common.dao.HibernateDao;
import com.mimosa.common.system.role.RoleType;
import com.mimosa.util.config.Page;
import com.mimosa.util.config.YesNoType;

@Repository
public class TUserDao extends HibernateDao<TUser, String> {

	/**
	 * 根据登录名判断数据库中是否存在这个用户
	 * 
	 * @return
	 */
	public boolean getUserByName(String name) {
		boolean flag = false;
		TUser user = this.findUnique("from TUser u where u.state = ? and u.userName = ? ", YesNoType.Y.name(), name);
		if (user == null) {
			return flag;
		} else {
			flag = true;
		}
		return flag;
	}

	/**
	 * @author 周恪竭
	 * @param name
	 *            用户名
	 * @param password
	 *            登录名
	 * @return 根据用户名和密码获取用户,如果密码不正确返回为null
	 */
	public boolean getUserByNameMM(String name, String password) {
		boolean flag = false;
		List<TUser> list = this.find("from TUser u where u.userName =? and u.userPass = ? and u.state = ?", name,
				password, YesNoType.Y.name());
		if (list == null || list.size() == 0) {
			return flag;
		} else {
			flag = true;
		}
		return flag;

	}

	/**
	 * 修改用户信息
	 * 
	 * @param user
	 */
	public void updateUser(TUser user) {
		Session s = this.getSession();
		s.clear();
		this.save(user);
	}

	/**
	 * @author 周恪竭 获取所有的用户信息
	 * @param column
	 *            数据库表的字段
	 * @param ReceiverId
	 *            对应字段值
	 * @return 封装好的收到信息的对象的集合
	 */
	@SuppressWarnings("unchecked")
	public List<TUser> getUserList(String hql, int pageSize, int pageNo) {
		Session s = this.getSession();
		Query query = s.createQuery(hql);
		query.setFirstResult(pageNo);
		query.setMaxResults(pageSize);
		return query.list();
	}

	/**
	 * @author 周恪竭
	 * @param user
	 *            用户对象
	 */
	public void saveUser(TUser user) {
		user.setState(YesNoType.Y.name());
		this.save(user);

	}

	/**
	 * 删除用户
	 * 
	 * @author yunjum
	 * @param id
	 * 
	 */
	public String deleteUser(String id) {
		// 逻辑删除，把它的标识位设置为0
		final String hql = "update TUser u  set u.state= :s where id =:id";
		this.getSession().createQuery(hql).setParameter("s", YesNoType.N.name()).setParameter("id", id).executeUpdate();
		String userName = this.get(id).getRealName();
		// this.delete(id);
		return userName;

	}
	public String deleteUserById(String id) {
		// 逻辑删除，把它的标识位设置为0
		final String hql = "delete from TUser u  where u.id =:id";
		this.getSession().createQuery(hql).setParameter("id", id).executeUpdate();
		return id;

	}

	/**
	 * @author zuzhilong
	 * @date 2013-05-06
	 * @param userName
	 *            用户名
	 * @return 根据用户名进行模糊查询分页进行查询获取所有的用户信息
	 */
	@SuppressWarnings("unchecked")
	public List<TUser> getUserListByName(String userName, String hql, int pageSize, int pageNo) {
		Session s = this.getSession();
		hql += "from TUser u where u.state = '" + YesNoType.Y.name() + "'";
		if (StringUtils.isNotBlank(userName)) {
			hql += " and u.userName like " + '%' + userName + '%';
		}
		Query query = s.createQuery(hql);
		query.setFirstResult(pageNo);
		query.setMaxResults(pageSize);
		return query.list();
	}

	/**
	 * 根据角色id获取所有和该角色相关联的人员
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getInThisRoleUsers(Page<TUser> page, String roleId) {
		// 获取所有和该角色相关联的人员
		Page<TUser> yUserList = this.findPage(page,
				"from TUser u where u.id in (select t.userId from TUserRole t where t.roleId = ?) and u.state = ?",
				roleId, YesNoType.Y.name());
		return yUserList;
	}

	/**
	 * 根据角色id获取所有和该角色相关联的人员
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getInThisGroupUsers(Page<TUser> page, String groupId) {
		// 获取所有和该角色相关联的人员
		Page<TUser> yUserList = this.findPage(page,
				"select user from TUser user left join user.gorupList as group with group.id=? where user.state = ?",
				groupId, YesNoType.Y.name());
		return yUserList;
	}

	/**
	 * 根据角色id获取所有和该角色相关联的人员
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getInThisRoleUsers(Page<TUser> page, String roleId, String userUserName, String userRealName) {
		// 获取所有和该角色相关联的人员
		Page<TUser> yUserList = null;
		if (StringUtils.isNotBlank(userUserName) && StringUtils.isNotBlank(userRealName)) {
			yUserList = this.findPage(page,
					"from TUser u where u.id in (select t.userId from TUserRole t where t.roleId = ?) and u.userName like ? and u.realName like ? and u.state = ?",
					roleId, '%' + userUserName + '%', '%' + userRealName + '%', YesNoType.Y.name());
		} else if (StringUtils.isNotBlank(userUserName)) {
			yUserList = this.findPage(page,
					"from TUser u where u.id in (select t.userId from TUserRole t where t.roleId = ?) and u.userName like ? and u.state = ?",
					roleId, '%' + userUserName + '%', YesNoType.Y.name());
		} else {
			yUserList = this.findPage(page,
					"from TUser u where u.id in (select t.userId from TUserRole t where t.roleId = ?) and u.realName like ? and u.state = ?",
					roleId, '%' + userRealName + '%', YesNoType.Y.name());
		}
		return yUserList;
	}

	/**
	 * 根据角色id获取所有和该角色相关联的人员
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getInThisGroupUsers(Page<TUser> page, String groupId, String userName) {
		// 获取所有和该角色相关联的人员
		Page<TUser> yUserList = this.findPage(page,
				"select user from TUser user left join user.groupList as group with group.id=? where user.realName like ? and user.state = ?",
				groupId, '%' + userName + '%', YesNoType.Y.name());
		return yUserList;
	}

	/**
	 * 获取不在该角色下的用户们
	 * 
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getNotInThisRoleUsers(Page<TUser> page, String roleId) {
		Page<TUser> wUserList = findPage(page,
				"from TUser u where u.id not in(select t.userId from TUserRole t where t.roleId = ?) and u.state = ?",
				roleId, YesNoType.Y.name());
		return wUserList;
	}

	/**
	 * 根据角色id和搜索条件在角色外进行搜索
	 * 
	 * @author
	 * @author wangchao
	 * @param roleId
	 *            角色id
	 * @return
	 */
	public Page<TUser> getNotInThisRoleUsers(Page<TUser> page, String roleId, String userName, String realName) {
		Page<TUser> wUserList = null;
		if (StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(realName)) {
			findPage(page,
					"from TUser u where u.id not in(select t.userId from TUserRole t where t.roleId = ?) and u.userName like ? and u.realName like ? and  u.state = ? ",
					roleId, '%' + userName + '%', '%' + realName + '%', YesNoType.Y.name());
		} else if (StringUtils.isNotBlank(userName)) {
			findPage(page,
					"from TUser u where u.id not in(select t.userId from TUserRole t where t.roleId = ?) and u.userName like ? and  u.state = ? ",
					roleId, '%' + userName + '%', YesNoType.Y.name());
		} else {
			findPage(page,
					"from TUser u where u.id not in(select t.userId from TUserRole t where t.roleId = ?) and u.realName like ? and  u.state = ? ",
					roleId, '%' + realName + '%', YesNoType.Y.name());
		}
		return wUserList;
	}

	/**
	 * 查找指定组内的用户列表
	 * 
	 * @param page
	 * @param groupId
	 * @param userName
	 * @return
	 */
	public Page<TUser> getNotInThisGroupUsers(Page<TUser> page, String groupId, String userName) {
		Page<TUser> wUserList = findPage(page,
				"select user from TUser as user left join user.groupList as group where (group.id is null OR group.id!=?) and  user.state = ? and user.realName like ?",
				groupId, YesNoType.Y.name(), '%' + userName + '%');
		return wUserList;
	}

	/**
	 * 查找指定组内的用户列表
	 * 
	 * @param page
	 * @param groupId
	 * @param userName
	 * @return
	 */
	public Page<TUser> getNotInThisGroupUsers(Page<TUser> page, String groupId) {
		Page<TUser> wUserList = findPage(page,
				"select user from TUser as user left join user.groupList as group where (group.id is null OR group.id!=?) and user.state = ?",
				groupId, YesNoType.Y.name());
		return wUserList;
	}

	/**
	 * 根据部门id得到用户列表
	 * 
	 * @param departmentId
	 * @return
	 */
	public List<TUser> getUserByDepId(String departmentId) {
		String hql = "from TUser where department.id = '" + departmentId + "'";
		return this.find(hql);
	}

	/**
	 * 查询所有未逻辑删除用户
	 * 
	 * @return
	 */
	public List<TUser> getAllByState() {
		String hql = "from TUser where state = ?";
		return this.find(hql, YesNoType.Y.name());
	}

	// 获取系统中所有的工人
	public List<TUser> getWorkers() {
		String hql = "from TUser u where u.id in(select t.userId from TUserRole t where t.roleId = ?)";
		return this.find(hql, RoleType.WORKER.name());
	}
}